package com.terios.logic.ui;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.terios.R;
import com.terios.conts.CommonStrs;
import com.terios.logic.MainService;
import com.terios.logic.Task;
import com.terios.util.Exit;

/**
 * HomeActivity.java 2012-9-27 下午12:20:51
 * 
 * @author Wang Tengrui
 */
public class MainActivity extends TabActivity {
	public static final int REFRESH_WEIBO = 1;

	public static final String TAB_HOME = "TabHome";
	public static final String TAB_MSG = "TabMsg";
	public static final String TAB_SEARCH = "TabSearch";
	public static final String TAB_USERDATA = "TabUsreData";
	public static final String TAB_MORESET = "TabMorSet";

	public TabHost tabHost1;
	public static RadioGroup indexGroup;

	private ProgressDialog refresh;

	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(getWindow().FEATURE_NO_TITLE);// 取消标题显示
		setContentView(R.layout.maintable);
		RadioButton radio_button0 = (RadioButton) this.findViewById(R.id.radio_button0);
		radio_button0.setChecked(true);// 默认选择首页按钮是按下状态
		radio_button0.setOnClickListener(new OnClickListener() {// 点击首页按钮的时候刷新信息

					@Override
					public void onClick(View v) {
						int nowPage = 1;

						HashMap<String, Integer> paramHashMap = new HashMap<String, Integer>();
						paramHashMap.put("nowPage", Integer.valueOf(nowPage));
						paramHashMap.put("pageSize", CommonStrs.MAX_INFO_NUM);

						// 加载主页微博信息任务
						Task task = new Task(CommonStrs.TASK_GET_USER_HOMETIMEINLINE, paramHashMap);
						MainService.allTask.add(task);
						
						HomeActivity home = (HomeActivity) MainService.getActivityByName(HomeActivity.getMyName(false));
						if (home != null) {
							ProgressBar progress=(ProgressBar) home.findViewById(R.id.titleprogressBar);
							progress.setVisibility(View.VISIBLE);
						}
					}
				});
		tabHost1 = this.getTabHost();
		// tabHost1子标签跳转到HomeActivity
		TabSpec tabSpec1 = tabHost1.newTabSpec(TAB_HOME)// 设定标签
				.setIndicator(TAB_HOME);// 指定一个标签作为选项卡指示符
		// 指定一个加载activity的Intent对象作为选项卡内容
		tabSpec1.setContent(new Intent(MainActivity.this, HomeActivity.class));
		tabHost1.addTab(tabSpec1);// 添加第一个子页

		TabSpec tabSpec2 = tabHost1.newTabSpec(TAB_MSG).setIndicator(TAB_MSG);
		// tabHost1子标签跳转到MSGActivity
		tabSpec2.setContent(new Intent(MainActivity.this, MSGActivity.class));
		tabHost1.addTab(tabSpec2);// 第二个子页

		TabSpec tabSpec3 = tabHost1.newTabSpec(TAB_USERDATA).setIndicator(TAB_USERDATA);
		// tabHost1子标签跳转到UserInfoActivity
		tabSpec3.setContent(new Intent(MainActivity.this, UserInfoActivity.class));
		tabHost1.addTab(tabSpec3);// 第三个子页

		TabSpec tabSpec4 = tabHost1.newTabSpec(TAB_SEARCH).setIndicator(TAB_SEARCH);
		// tabHost1子标签跳转到SearchActivity
		tabSpec4.setContent(new Intent(MainActivity.this, SearchActivity.class));
		tabHost1.addTab(tabSpec4);// 第四个子页

		TabSpec tabSpec5 = tabHost1.newTabSpec(TAB_MORESET).setIndicator(TAB_MORESET);
		// tabHost1子标签跳转到MoreSetting
		tabSpec5.setContent(new Intent(MainActivity.this, MoreSetting.class));
		tabHost1.addTab(tabSpec5);// 第四个子页

		this.indexGroup = (RadioGroup) this.findViewById(R.id.main_radio);
		// RadioGroup的子选项点击监听
		indexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.radio_button0:
					tabHost1.setCurrentTabByTag(TAB_HOME);
					break;
				case R.id.radio_button1:
					tabHost1.setCurrentTabByTag(TAB_MSG);
					break;
				case R.id.radio_button2:
					tabHost1.setCurrentTabByTag(TAB_SEARCH);
					break;
				case R.id.radio_button3:
					tabHost1.setCurrentTabByTag(TAB_USERDATA);
					break;
				case R.id.radio_button4:
					tabHost1.setCurrentTabByTag(TAB_MORESET);
					break;
				}
			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Exit.btexit(MainActivity.this);// 当我们按下返回键的时候要执行的动作
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	/**
	 * 返回本activity的名字
	 * @return
	 * @author zxx
	 */
	public static String getMyName(boolean isShowPackage){
		if(isShowPackage){
			return MainActivity.class.getPackage()+MainActivity.class.getName();
		}else{
			return MainActivity.class.getName();
		}
	}
}
